import {dialog} from "@/components/dialog/dialog.js"
export const dialogs = {
	/** 缓存池
	   key:标识，
	   time：同时触发的时间范围校验
	*/
	local(key, time = 30) {
		let isAction = true;

		let local = {
			key,
			dateTime: new Date().getTime()
		}
		let localList = uni.getStorageSync('localList') || []
		if (!localList.length) {
			localList.push(local)
			uni.setStorageSync('localList',localList)
		} else {
			localList.forEach(item => {
				if (item.key == local.key && (local.dateTime - item.dateTime) < (time * 1000)) {
					isAction = false
				}
			})
			if(isAction){
				localList.forEach(item => {
					if (item.key == local.key ) {
						item.dateTime = local.dateTime
					} 
				})
				uni.setStorageSync('localList',localList)
			}
		}
		return isAction;
	},

	alert(obj) {
		let defValue = {
			title: "提示",
			content: "这是一条提示",
			confirmText: '确定',
			actionCode: '',
			age: '',
			gender: '',
			ipProvince: '',
			gameName: '',
			image:"http://aliyun_id_photo_bucket.oss.aliyuncs.com/default_handsome.jpg",
			key: '',
			time: 30
		}
		defValue={...defValue,...obj}
		let {title,image,content,confirm,actionCode,age,gender,ipProvince,gameName,confirmText,key,time}=defValue
		if (key && !this.local(key, time)) return false;
		// #ifdef APP-PLUS
		dialog.alert({
			content,
			title,
			image,
			actionCode,
			age,
			gender,
			ipProvince,
			gameName,
			confirmText
		}, confirm)
		// #endif
		// #ifndef APP-PLUS
		uni.showModal({
			title,
			content,
			image,
			actionCode,
			age,
			gender,
			ipProvince,
			gameName,
			confirmText,
			showCancel: false,
			confirmColor: "#e03c31",
			success: confirm
		})
		// #endif
	},
	/**
	 * 确认提示框
	 */
	confirm(obj) {
		let defValue = {
			title: "提示",
			content: "这是一条可选择的确认框",
			confirm,
			actionCode,
			age,
			gender,
			ipProvince,
			gameName,
			confirmText: '确定',
			cancel,
			cancelText:'取消',
			key: '',
			time: 30
		}
		defValue={...defValue,...obj}
		let {title,content,confirm,confirmText,cancel,cancelText,key,time}=defValue
		if (key && !this.local(key, time)) return false;
		// #ifdef APP-PLUS
		dialog.confirm({
			content,
			title,
			img,
			confirmText,
			actionCode,
			age,
			gender,
			ipProvince,
			gameName,
			cancelText,
		}, confirm, cancel)
		// #endif
		// #ifndef APP-PLUS
		uni.showModal({
			title,
			content,
			cancelText,
			actionCode,
			age,
			gender,
			ipProvince,
			gameName,
			confirmText,
			confirmColor: "#e03c31",
			success: (e) => {
				if (e.confirm) {
					confirm && confirm()
				} else if (e.cancel) {
					cancel && cancel()
				}
			},
			fail: (e) => {
				console.log(e)
			}
		})
		// #endif
	},
}